package cn.cloud9.chinese.hongkong.util;

import cn.cloud9.chinese.hongkong.constant.Constant;
import cn.cloud9.chinese.hongkong.po.HkRegionPO;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.http.HttpUtil;
import lombok.SneakyThrows;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.time.LocalDateTime;

/**
 * @projectName: 行政区域爬取工具
 * @author: Cloud9
 * @date: 2022年07月13日 09:37
 * @version: 1.0
 */
public class HkRegionUtil {

    private final static Db db = Db.use();

    @SneakyThrows
    private static void writeToDB(HkRegionPO po) {
        db.insert(
            Entity.create("`hk-region`")
            .set("ID", po.getId())
            .set("PARENT_ID", po.getParentId())
            .set("NAME", po.getName())
            .set("LINK", po.getLink())
            .set("FULL_PATH", po.getFullPath())
            .set("LEVEL", po.getLevel())
            .set("GEN_TIME", LocalDateTime.now())
        );
    }


    public static void readHkRegionData() {
        Document doc = Jsoup.parse(HttpUtil.get(Constant.HONGKONG_REGION_PAGE));

        final Elements regionDivList = doc.getElementsByClass("index_region_inner");
        regionDivList.forEach(regionDiv -> {
            // 对应[香港岛，九龙，新界，离岛]
            String regionName = regionDiv.select("h2.region_label").get(0).text();
            int regionIndex = regionDivList.indexOf(regionDiv) + 1;
            HkRegionPO regionPO = HkRegionPO
                    .builder()
                    .id(regionIndex + 10)
                    .parentId(0)
                    .name(regionName)
                    .level(1)
                    .build();
            writeToDB(regionPO);

            Elements sectorList = regionDiv.select("div.district_item");
            sectorList.forEach(sector -> {
                String sectorName = sector.select("h3.district_label").get(0).text();
                int sectorIndex = sectorList.indexOf(sector) + 1;
                HkRegionPO sectorPo = HkRegionPO
                        .builder()
                        .id(
                            Integer.valueOf(
                                10 > sectorIndex ?
                                regionPO.getId() + "0" + sectorIndex
                                : regionPO.getId() + "" + sectorIndex
                            )
                        )
                        .parentId(regionPO.getId())
                        .name(sectorName)
                        .fullPath(regionName + " -> " + sectorName)
                        .level(2)
                        .build();
                writeToDB(sectorPo);
                Elements aTagList = sector.select("ul.subdistrict_list > li > a");

                aTagList.forEach(aTag -> {
                    int villageIndex = aTagList.indexOf(aTag) + 1;
                    String villageName = aTag.text();
                    String href = aTag.attr("href");
                    href = Constant.BASE_URL + href;
                    HkRegionPO villagePo = HkRegionPO
                            .builder()
                            .id(
                                Integer.valueOf(
                                    10 > villageIndex ?
                                    sectorPo.getId() + "0" + villageIndex
                                    : sectorPo.getId() + "" + villageIndex
                                )
                            )
                            .parentId(sectorPo.getId())
                            .name(villageName)
                            .link(href)
                            .fullPath(regionName + " -> " + sectorName + " -> " + villageName)
                            .level(3)
                            .build();
                    writeToDB(villagePo);

                    Document roadDoc = Jsoup.parse(HttpUtil.get(villagePo.getLink()));

                    Elements roadTags = roadDoc.select("div.subdistrict_area span.street_link > a");
                    roadTags.forEach(roadTag -> {
                        int roadIndex = roadTags.indexOf(roadTag) + 1;
                        String text = roadTag.text();
                        String tagHref = roadTag.attr("href");
                        HkRegionPO roadPo = HkRegionPO
                            .builder()
                            .id(
                                Integer.valueOf(
                                    10 > villageIndex ?
                                    villagePo.getId() + "0" + roadIndex
                                    : villagePo.getId() + "" + roadIndex
                                )
                            )
                            .parentId(villagePo.getId())
                            .name(text)
                            .link(tagHref)
                            .fullPath(regionName + " -> " + sectorName + " -> " + villageName + " -> " + text)
                            .level(4)
                            .build();
                        writeToDB(roadPo);
                    });

                });
            });

        });

    }


}
